const { getEventData } = getApp()
Component({
  options: {
    addGlobalClass: true
  },
  properties: {
    options: Array
  },
  data: {
    currentData: []
  },
  methods: {
    onItemChange(event) {
      const { index } = getEventData(event)
      const value = +event.detail.value;
      const item = this.data.options[index].options[value];
      this.setData({
        [`currentData[${index}]`]: {
          valueKey: this.data.options[index].valueKey,
          nodeKey: this.data.options[index].nodeKey,
          data: item
        }
      }, () => {
        this.bindChange()
      })
    },

    bindChange() {
      const data = this.data.currentData;

      const params = {};
      data.forEach(item => {
        if (item) {
          params[item.nodeKey] = item.data[item.nodeKey]
        }
      })
      this.triggerEvent('change', params)
    }
  }
})
